package http

import (
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	"gitee.com/captials-team/ubdframe/src/domain/dto"
	"gitee.com/captials-team/ubdframe/src/domain/dto/paginate"
	"gitee.com/captials-team/ubdframe/src/domain/dto/reqdata"
	"gitee.com/captials-team/ubdframe/src/domain/dto/respdata"
	"gitee.com/captials-team/ubdframe/src/domain/interfaces"
	"gitee.com/captials-team/ubdframe/src/domain/models"
	"gitee.com/captials-team/ubdframe/src/pkg/gin_http"
	v1log "gitee.com/captials-team/ubdframe/src/pkg/logs"
	"github.com/gin-gonic/gin"
	"go.uber.org/dig"
)

type BlockManageController struct {
	l v1log.ILog

	conf *configstc.BlockAppConfig

	blockSvc interfaces.ItfBlockContentService

	gin_http.ResponseController
}

func NewBlockManageController(di *dig.Container, l v1log.ILog, conf *configstc.BlockAppConfig, svc interfaces.ItfBlockContentService) *BlockManageController {
	ctr := &BlockManageController{
		l:    l,
		conf: conf,
	}

	ctr.blockSvc = svc

	return ctr
}

// SearchBlock 块列表 godoc
// @Summary      获取块列表
// @Description  获取块列表
// @Tags         block
// @Produce      json
// @Security     AdminKeyAuth
// @Param        param    body     reqdata.SearchBlockContentParam  true  "参数"
// @success 0  {object} respdata.ResponseData{data=respdata.SearchListResponse{list=[]models.BlockContent}} "获取成功"
// @success 500 {object} respdata.ResponseData{} "获取失败"
// @Router       /mag/block/search [post]
func (ctr *BlockManageController) SearchBlock(ctx *gin.Context) {
	var req reqdata.SearchBlockContentParam
	ctx.ShouldBind(&req)

	params := &dto.SearchBlockContentParams{
		Keywords: req.Keywords,
		Status:   req.Status,
		Code:     req.Code,
		Type:     req.Type,
	}

	list, pager, err := ctr.blockSvc.Search(params, &paginate.Pager{
		Page: req.Page,
		Size: req.Size,
	})
	if err != nil {
		ctr.l.Error("search err %s", err)
		ctr.Response(ctx, respdata.CError)
		return
	}

	ctr.Response(ctx, respdata.CSuccess.MData(respdata.SearchListResponse{
		List:     list,
		Paginate: *pager,
	}))
}

// QueryBlock  godoc
// @Summary      查询指定块
// @Description  查询指定单个块内容
// @Tags         block
// @Security     AdminKeyAuth
// @Produce      json
// @Param        param    body     reqdata.QueryBlockReq  true  "查询指定id详情"
// @success 0  {object} respdata.ResponseData{data=models.BlockContent} "获取成功"
// @success 2  {object} respdata.ResponseData{} "系统繁忙"
// @success 4  {object} respdata.ResponseData{} "参数无效"
// @success 10  {object} respdata.ResponseData{} "未查询到"
// @Router       /mag/block/detail [post]
func (ctr *BlockManageController) QueryBlock(ctx *gin.Context) {
	var req reqdata.QueryBlockReq
	ctx.ShouldBind(&req)

	detail, err := ctr.blockSvc.Query(req.Id, req.Code)
	if err != nil {
		ctr.l.Error("query fail %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}

	ctr.Response(ctx, respdata.CSuccess.MData(detail))
}

// SaveBlock  godoc
// @Summary      创建或编辑块
// @Description  创建或编辑块
// @Tags         block
// @Security     AdminKeyAuth
// @Produce      json
// @Param        param    body     reqdata.SaveBlockReq  true  "提交用户信息"
// @success 0  {object} respdata.ResponseData{} "保存成功"
// @success 2  {object} respdata.ResponseData{} "系统繁忙"
// @success 4  {object} respdata.ResponseData{} "参数无效"
// @success 10  {object} respdata.ResponseData{} "未查询到"
// @Router       /mag/block/save [post]
func (ctr *BlockManageController) SaveBlock(ctx *gin.Context) {
	var req reqdata.SaveBlockReq
	ctx.ShouldBind(&req)
	_, err := ctr.blockSvc.Save(req.Id, &models.BlockContent{
		Code:    req.Code,
		Name:    req.Name,
		Type:    req.Type,
		Content: req.Content,
	})
	if err != nil {
		ctr.l.Error("save fail %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}
	ctr.Response(ctx, respdata.CSuccess)
	return
}

// DeleteBlock  godoc
// @Summary      删除指定块
// @Description  删除指定块
// @Tags         block
// @Security     AdminKeyAuth
// @Produce      json
// @Param        param    body     reqdata.DetailReq  true  "指定用户id"
// @success 0  {object} respdata.ResponseData{} "成功"
// @Router       /mag/block/delete [post]
func (ctr *BlockManageController) DeleteBlock(ctx *gin.Context) {
	var req reqdata.DetailReq
	ctx.ShouldBind(&req)
	if req.Id <= 0 {
		ctr.Response(ctx, respdata.CParamsInvalid)
		return
	}

	_, err := ctr.blockSvc.Delete(req.Id)
	if err != nil {
		ctr.l.Error("query fail %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}

	ctr.Response(ctx, respdata.CSuccess)
}

// DisableBlock  godoc
// @Summary      启用/禁用块
// @Description  启用/禁用块
// @Tags         block
// @Security     AdminKeyAuth
// @Produce      json
// @Param        param    body     reqdata.DetailReq  true  "指定用户id"
// @success 0  {object} respdata.ResponseData{} "成功"
// @Router       /mag/block/disable [post]
func (ctr *BlockManageController) DisableBlock(ctx *gin.Context) {
	ctr.Response(ctx, respdata.CSuccess)
	return
}
